//
//  LoginVC.m
//  genericframework
//
//  Created by 谷丰 on 2025/3/29.
//

#import "LoginVC.h"
<<<<<<< HEAD
#import "RegAccountVC.h"
#import "ForgetPasswordVC.h"
@interface LoginVC ()
@property (nonatomic,strong)UIImageView * BgImageView;//登录背景
@property (nonatomic,strong)UIImageView * LogoImageView;//Logo
@property (nonatomic,strong)UITextField *AccountText;//账号
@property (nonatomic,strong)UITextField *PassWordText;//密码
@property (nonatomic, strong) UIButton *registerButton;    // 快速注册
@property (nonatomic, strong) UIButton *forgetPwdButton;   // 忘记密码
@property (nonatomic, strong) UIButton *loginButton;       // 登录按钮
@property (nonatomic, strong) UIButton *checkBox;          // 勾选框
@property (nonatomic, strong) UILabel *agreementLabel;     // 协议文本（包含可点击链接）
@property (nonatomic, assign) BOOL isAgreed;  // 是否同意协议

@property (nonatomic,strong)  UIView * LineView;
@property (nonatomic,strong)  UIView * RightLineView;
@property (nonatomic,strong)  UILabel * OrLabel;
@property (nonatomic, strong) UIButton *wechatLoginButton;
@property (nonatomic, strong) UIButton *qqLoginButton;
@property (nonatomic, strong) UIButton *appleLoginButton;
=======

@interface LoginVC ()
>>>>>>> 291cab90cbb8ff4921e32ec95b13330bf79c214c

@end

@implementation LoginVC
<<<<<<< HEAD
// 快速注册按钮（左对齐）
- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerButton setTitle:GFString(@"快速注册") forState:UIControlStateNormal];
        _registerButton.titleLabel.font = GFFont(12);
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _registerButton.backgroundColor = [UIColor clearColor];
        _registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        // 添加点击事件
        [_registerButton addTarget:self action:@selector(registerButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_registerButton];
    }
    return _registerButton;
}

// 忘记密码按钮（右对齐）
- (UIButton *)forgetPwdButton {
    if (!_forgetPwdButton) {
        _forgetPwdButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetPwdButton setTitle:GFString(@"忘记密码") forState:UIControlStateNormal];
        _forgetPwdButton.titleLabel.font = GFFont(12);
        [_forgetPwdButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _forgetPwdButton.backgroundColor = [UIColor clearColor];
        _forgetPwdButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        
        // 添加点击事件
        [_forgetPwdButton addTarget:self action:@selector(forgetPwdButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_forgetPwdButton];
    }
    return _forgetPwdButton;
}

// 登录按钮（透明背景 + 白色边框 + 圆角）
- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:GFString(@"登录") forState:UIControlStateNormal];
        _loginButton.titleLabel.font = GFFont(14);
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginButton.backgroundColor = [BaseWay MainColor];
        
        // 设置圆角和边框
        _loginButton.layer.cornerRadius = FitSizeW(20);  // 高度30，圆角15
        _loginButton.layer.borderWidth = 1.0;
        _loginButton.layer.borderColor = [BaseWay MainColor].CGColor;
        
        // 添加点击事件
        [_loginButton addTarget:self action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}

-(UIImageView *)BgImageView{
    if (!_BgImageView) {
        _BgImageView = [[UIImageView alloc]init];
        _BgImageView.image = [UIImage imageNamed:@"背景"];
        _BgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:_BgImageView];
    }
    return _BgImageView;
}
-(UIImageView *)LogoImageView{
    if (!_LogoImageView) {
        _LogoImageView = [[UIImageView alloc]init];
        _LogoImageView.image = [UIImage imageNamed:@"logo"];
        _LogoImageView.contentMode = UIViewContentModeScaleAspectFill;
        _LogoImageView.layer.cornerRadius=FitSizeW(11);
        _LogoImageView.clipsToBounds=YES;
        [self.view addSubview:_LogoImageView];
    }
    return _LogoImageView;
}
-(UITextField *)AccountText{
    if(!_AccountText){
        _AccountText = [[UITextField alloc]init];
        _AccountText.textColor=rgba(255, 255, 255, 1);
        _AccountText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:GFString(@"请输入爱诺账号") attributes:@{NSForegroundColorAttributeName: rgba(255, 255, 255, 1),NSFontAttributeName: GFFont(14)}];
        _AccountText.layer.cornerRadius = FitSizeW(20);
        _AccountText.font = GFFont(14);
        _AccountText.layer.borderColor = rgba(255, 255, 255, 1).CGColor;
        _AccountText.layer.borderWidth=1;
        [self.view addSubview:_AccountText];
    }
    return _AccountText;
}
-(UITextField *)PassWordText{
    if(!_PassWordText){
        _PassWordText = [[UITextField alloc]init];
        _PassWordText.textColor=rgba(255, 255, 255, 1);
        _PassWordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:GFString(@"请输入密码") attributes:@{NSForegroundColorAttributeName: rgba(255, 255, 255, 1),NSFontAttributeName: GFFont(14)}];
        _PassWordText.layer.cornerRadius = FitSizeW(20);
        _PassWordText.secureTextEntry= YES;
        _PassWordText.clearsOnBeginEditing  =YES;
        _PassWordText.clearButtonMode = UITextFieldViewModeAlways;
        _PassWordText.font = GFFont(14);
        _PassWordText.layer.borderColor = rgba(255, 255, 255, 1).CGColor;
        _PassWordText.layer.borderWidth=1;
        [self.view addSubview:_PassWordText];
    }
    return _PassWordText;
}
#pragma mark - 懒加载按钮
-(UIView *)LineView{
    if (!_LineView) {
        _LineView = [[UIView alloc]init];
        _LineView.backgroundColor=rgba(255, 255, 255, 1);
        [self.view addSubview:_LineView];
    }
    return _LineView;
}
-(UIView *)RightLineView{
    if (!_RightLineView) {
        _RightLineView = [[UIView alloc]init];
        _RightLineView.backgroundColor=rgba(255, 255, 255, 1);
        [self.view addSubview:_RightLineView];
    }
    return _RightLineView;
}
-(UILabel *)OrLabel{
    if (!_OrLabel) {
        _OrLabel = [[UILabel alloc]init];
        _OrLabel.textColor=rgba(255, 255, 255, 1);
        _OrLabel.font = GFBFont(18);
        _OrLabel.textAlignment=NSTextAlignmentCenter;
        _OrLabel.text = GFString(@"Or");
        [self.view addSubview:_OrLabel];
    }
    return _OrLabel;
}
- (UIButton *)wechatLoginButton {
    if (!_wechatLoginButton) {
        _wechatLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_wechatLoginButton setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
        _wechatLoginButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_wechatLoginButton addTarget:self action:@selector(wechatLoginAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_wechatLoginButton];
    }
    return _wechatLoginButton;
}

- (UIButton *)qqLoginButton {
    if (!_qqLoginButton) {
        _qqLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qqLoginButton setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
        _qqLoginButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_qqLoginButton addTarget:self action:@selector(qqLoginAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_qqLoginButton];
    }
    return _qqLoginButton;
}

- (UIButton *)appleLoginButton {
    if (!_appleLoginButton) {
        _appleLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_appleLoginButton setImage:[UIImage imageNamed:@"icon_苹果 拷贝"] forState:UIControlStateNormal];
        _appleLoginButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_appleLoginButton addTarget:self action:@selector(appleLoginAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_appleLoginButton];
    }
    return _appleLoginButton;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.BgImageView.frame= CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self CompositionNavBarHidden:NO andHiddenBackButton:YES withTitle:GFString(@"登录")];
    self.BaseNavBarView.backgroundColor=[UIColor clearColor];
    self.LogoImageView.frame=CGRectMake(SCREEN_WIDTH/2-FitSizeW(30), NAVIGATION_BAR_HEIGHT+FitSizeW(53), FitSizeW(60), FitSizeW(60));
    self.AccountText.frame=CGRectMake(FitSizeW(70), CGRectGetMaxY(self.LogoImageView.frame)+FitSizeW(40), SCREEN_WIDTH-FitSizeW(140), FitSizeW(40));
    [BaseWay Textfiled:self.AccountText AndViewWithImage:@"icon-登录账号" andWithType:@"left"];
    self.PassWordText.frame=CGRectMake(FitSizeW(70), CGRectGetMaxY(self.AccountText.frame)+FitSizeW(22), SCREEN_WIDTH-FitSizeW(140), FitSizeW(40));
    [BaseWay Textfiled:self.PassWordText AndViewWithImage:@"icon-密码" andWithType:@"left"];
    
    self.registerButton.frame=CGRectMake(FitSizeW(90), CGRectGetMaxY(self.PassWordText.frame)+FitSizeW(20), (SCREEN_WIDTH-FitSizeW(200))/2, FitSizeW(33));
    self.forgetPwdButton.frame=CGRectMake(CGRectGetMaxX(self.registerButton.frame)+FitSizeW(20), CGRectGetMaxY(self.PassWordText.frame)+FitSizeW(20), (SCREEN_WIDTH-FitSizeW(200))/2, FitSizeW(33));
    self.loginButton.frame=CGRectMake(FitSizeW(70), CGRectGetMaxY(self.forgetPwdButton.frame)+FitSizeW(20), SCREEN_WIDTH-FitSizeW(140), FitSizeW(40));
    self.isAgreed = NO;  // 默认未同意
        
    self.checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.checkBox setImage:[UIImage imageNamed:@"icon-未选择(1)"]  forState:UIControlStateNormal];
    [self.checkBox setImage:[UIImage imageNamed:@"icon-勾选 拷贝"] forState:UIControlStateSelected];
    [self.checkBox addTarget:self action:@selector(checkBoxTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkBox];
       // 使用 Auto Layout 布局
    self.checkBox.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.checkBox.topAnchor constraintEqualToAnchor:self.loginButton.safeAreaLayoutGuide.bottomAnchor constant:FitSizeW(31)],
        [self.checkBox.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:FitSizeW(70)],
        [self.checkBox.widthAnchor constraintEqualToConstant:FitSizeW(16)],
        [self.checkBox.heightAnchor constraintEqualToConstant:FitSizeW(16)]
    ]];
    self.agreementLabel = [[UILabel alloc] init];
    self.agreementLabel.userInteractionEnabled = YES;  // 允许交互
    self.agreementLabel.numberOfLines = 0;
       
       // 设置富文本
    NSString *fullText = GFString(@"我已阅读并同意《用户协议》和《隐私政策》");
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullText];
       
       // 设置整体文字颜色（黑色）
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, fullText.length)];
       
       // 设置《用户协议》为蓝色
    NSRange userAgreementRange = [fullText rangeOfString:GFString(@"《用户协议》")];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:userAgreementRange];
       
       // 设置《隐私政策》为蓝色
    NSRange privacyPolicyRange = [fullText rangeOfString:GFString(@"《隐私政策》")];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:privacyPolicyRange];
    [attributedString addAttribute:NSFontAttributeName value:GFFont(12) range:NSMakeRange(0, fullText.length)];
    self.agreementLabel.attributedText = attributedString;
    [self.view addSubview:self.agreementLabel];
       
       // 使用 Auto Layout 布局
    self.agreementLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.agreementLabel.centerYAnchor constraintEqualToAnchor:self.checkBox.centerYAnchor],
        [self.agreementLabel.leadingAnchor constraintEqualToAnchor:self.checkBox.trailingAnchor constant:FitSizeW(10)],
        [self.agreementLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-FitSizeW(20)]
    ]];
       
       // 添加点击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAgreementLabelTap:)];
    [self.agreementLabel addGestureRecognizer:tapGesture];
    
    
//三方登录
    self.LineView.frame=CGRectMake(FitSizeW(91), CGRectGetMaxY(self.loginButton.frame)+FitSizeW(169), (SCREEN_WIDTH-FitSizeW(225))/2, 1);
    self.OrLabel.frame=CGRectMake(CGRectGetMaxX(self.LineView.frame),CGRectGetMaxY(self.loginButton.frame)+FitSizeW(160) , FitSizeW(43), FitSizeW(19));
    self.RightLineView.frame=  CGRectMake(CGRectGetMaxX(self.OrLabel.frame), CGRectGetMaxY(self.loginButton.frame)+FitSizeW(169), (SCREEN_WIDTH-FitSizeW(225))/2, 1);
    
    self.wechatLoginButton.frame=CGRectMake(SCREEN_WIDTH/2-FitSizeW(92), CGRectGetMaxY(self.LineView.frame)+FitSizeW(34), FitSizeW(28), FitSizeW(28));
    self.qqLoginButton.frame=CGRectMake(SCREEN_WIDTH/2-FitSizeW(14), CGRectGetMaxY(self.LineView.frame)+FitSizeW(34), FitSizeW(28), FitSizeW(28));
    self.appleLoginButton.frame=CGRectMake(SCREEN_WIDTH/2+FitSizeW(64), CGRectGetMaxY(self.LineView.frame)+FitSizeW(34), FitSizeW(28), FitSizeW(28));
    // Do any additional setup after loading the view.
}

#pragma mark - 勾选框点击事件
- (void)checkBoxTapped {
    self.isAgreed = !self.isAgreed;
    self.checkBox.selected = self.isAgreed;
    NSLog(@"用户协议状态：%@", self.isAgreed ? @"已同意" : @"未同意");
}

#pragma mark - 协议文本点击事件
- (void)handleAgreementLabelTap:(UITapGestureRecognizer *)gesture {
    UILabel *label = (UILabel *)gesture.view;
    NSString *text = label.text;
    
    // 获取点击位置
    CGPoint tapLocation = [gesture locationInView:label];
    
    // 计算《用户协议》和《隐私政策》的文本范围
    NSRange userAgreementRange = [text rangeOfString:GFString(@"《用户协议》")];
    NSRange privacyPolicyRange = [text rangeOfString:GFString(@"《隐私政策》")];
    
    // 检测是否点击了《用户协议》
    if ([self isTapPoint:tapLocation inRange:userAgreementRange forLabel:label]) {
        NSLog(@"点击了《用户协议》");
        [self showUserAgreement];
        return;
    }
    
    // 检测是否点击了《隐私政策》
    if ([self isTapPoint:tapLocation inRange:privacyPolicyRange forLabel:label]) {
        NSLog(@"点击了《隐私政策》");
        [self showPrivacyPolicy];
        return;
    }
    
    // 如果点击其他部分，切换勾选状态
    [self checkBoxTapped];
}

// 判断点击位置是否在指定文本范围内
- (BOOL)isTapPoint:(CGPoint)point inRange:(NSRange)targetRange forLabel:(UILabel *)label {
    // 创建文本容器
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.bounds.size];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:label.attributedText];
    
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];
    
    // 计算目标文本的 rect
    NSRange glyphRange = [layoutManager glyphRangeForCharacterRange:targetRange actualCharacterRange:NULL];
    CGRect boundingRect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
    
    // 检查点击位置是否在范围内
    return CGRectContainsPoint(boundingRect, point);
}

#pragma mark - 跳转到协议页面
- (void)showUserAgreement {
    // 跳转用户协议页面（示例）
    NSLog(@"跳转到用户协议页面");
}

- (void)showPrivacyPolicy {
    // 跳转隐私政策页面（示例）
    NSLog(@"跳转到隐私政策页面");
}
#pragma mark - 按钮点击事件

- (void)wechatLoginAction {
    NSLog(@"微信登录");
    // 微信登录逻辑
}

- (void)qqLoginAction {
    NSLog(@"QQ登录");
    // QQ登录逻辑
}

- (void)appleLoginAction {
    NSLog(@"Apple登录");
    // Apple登录逻辑
}
- (void)registerButtonClicked {
    NSLog(@"点击了快速注册");
    
    RegAccountVC * reg = [[RegAccountVC alloc]init];
    reg.hidesBottomBarWhenPushed=YES;
    reg.AccountText =  self.AccountText.text?self.AccountText.text:@"";
    [self.navigationController pushViewController:reg animated:YES];
    // 跳转到注册页面
}

- (void)forgetPwdButtonClicked {
    NSLog(@"点击了忘记密码");
    ForgetPasswordVC* forget = [[ForgetPasswordVC alloc]init];
    forget.hidesBottomBarWhenPushed=YES;
    forget.AccountText =  self.AccountText.text?self.AccountText.text:@"";
    [self.navigationController pushViewController:forget animated:YES];
    // 跳转到找回密码页面
}

- (void)loginButtonClicked {
    NSLog(@"点击了登录");
    // 执行登录逻辑
}
=======

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

>>>>>>> 291cab90cbb8ff4921e32ec95b13330bf79c214c
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
